<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('helper.inc');
require_once('crypt.inc');
require_once('settings.inc');

$knopf = 0;
$func = $_REQUEST['func'];

# Auswertungsmenue
if (isset($_REQUEST['planung'])) { $knopf = 25; $headline = 29; }

$drm = array(true, false, false, false, false, true, false);

require_once('menu.inc');
require_once('knumber.inc');

# Folgende Funktion erlaubt die Auswahl einer Periode fuer die der Statusbericht
# erzeugt werden soll.
#
function Preselect() {
	global $periode;
	global $unum;
	global $menu;
	global $rstufe;

	$db = OpenDB();
?>
<form action="prplanhilf.php" method="post">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="headline" value="29">
<input type="hidden" name="func" value="prplan">

<table border=0>
<?php
	# Auswahl eines Projekts
	echo "<tr><td>Projektauswahl:</td>\n";
	echo "<td>";

	$query = "select pl_num, pl_prnum, pl_lfd, pr_name from plan, project ";
	$query .= "where pr_num = pl_prnum and pl_status in (2,3,4) ";
	$query .= "order by pl_prnum, pl_lfd desc";

	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	echo "<select name=\"pl_num\">\n";
	$numrows = numrowsDB($result);
	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $pl_num = $data[0];
	   $pl_prnum = $data[1];
	   $pl_lfd = $data[2];
	   $pr_name = $data[3];
	   echo "<option value=\"$pl_num\">$pl_lfd: $pl_prnum $pr_name</option>\n";
	   $rows++;
	}

	echo "</select>";
	echo "</td></tr>\n";
	echo "</table>\n";
	echo "<br><input type=\"submit\" name=\"submit\" value=\"Senden\"> <input type=\"reset\">\n";
	echo "</form>\n";
	closeDB($db);
}

function ShowPlanGesamt($print=false) {
	global $leneinheit;
	$pl_num = $_REQUEST['pl_num'];

	$db = OpenDB();
	# Tabelle Plan-Zusammenfassung
?>
<table width="40%" border=1>
   <tr>
      <td class="stbb">Gruppe</td>
      <td class="stbb">Jahr</td>
      <td class="stbb">Budget</td>
   </tr>
<?php
	# Plan auslesen (alle Tasks) und Aufwaende summieren
	$query = "select al_hours, al_pstart, kl_land from ";
	$query .= "allocation, mitarbeiter, task, key_land ";
	$query .= "where mi_num = al_ressource and kl_num = mi_land and ";
	$query .= "ta_num = al_task and ta_plnum = $pl_num ";
	$query .= "and ta_meeting = 0 ";
	$query .= "order by kl_land, al_pstart";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Der von Ihnen gew&auml;hlte Plan $pl_num enth&auml;lt keine Tasks!");
	   closeDB($db);
	   return;
	}

	$rows = 0;
	$sum = 0.0;
	$total = 0.0;
	$j = 0;
	$r = 0;
	$oldland = "";

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $al_hours = $data[0];
	   $al_pstart = $data[1];
	   $kl_land = $data[2];

	   $jahr = gmdate("Y", $al_pstart);

	   if ($rows == 0) {
	      $j = $jahr;
	      $oldland = $kl_land;
	   }

	   if ($j != $jahr || $oldland != $kl_land) {
	      printf ("<tr><td>$oldland</td><td align=\"center\">$j</td><td align=\"right\">%s</td></tr>\n", FormatNum($sum, 3));
	      $sum = 0;
	      $j = $jahr;
	      $oldland = $kl_land;
	      $r = $rows;
	   }

	   $sum += $al_hours;
	   $total += $al_hours;
	   $rows++;
	}

	if ($r != $rows)
	   printf ("<tr><td>$kl_land</td><td align=\"center\">$jahr</td><td align=\"right\">%s</td></tr>\n", FormatNum($sum, 3));

	# Anzeigen der Summe
	printf ("<tr><td colspan=2 class=\"stbb\">Summe:</td><td align=\"right\"><b>%s</b></td></tr>\n", FormatNum($total, 3));
	echo "</table>\n";
	closeDB($db);
}

function ShowPlanBasis($print=false) {
	global $leneinheit;
	global $phase;
	$pl_num = $_REQUEST['pl_num'];

	$db = OpenDB();
?>
<table width="80%" border=1>
   <tr>
<?
	if ($phase) {
	   echo "<td class=\"stbb\">Phase</td>\n";
	   echo "<td class=\"stbb\">Subphasen</td>\n";
	} else {
	   echo "<td class=\"stbb\">Task</td>\n";
	}
?>
      <td class="stbb">Aktivit&auml;t/T&auml;tigkeit</td>
      <td class="stbb">Gruppe</td>
      <td class="stbb">Monat</td>
      <td class="stbb">Jahr</td>
      <td class="stbb">Basis</td>
   </tr>
<?php
	# Plan auslesen (alle Tasks) und Aufwaende summieren
	$query = "select al_hours, al_pstart, al_phase, ta_name, ta_notiz,";
	$query .= "kl_land, kp_phase from ";
	$query .= "allocation, mitarbeiter, task, key_land, key_phase ";
	$query .= "where mi_num = al_ressource and kl_num = mi_land and ";
	$query .= "ta_num = al_task and kp_num = al_phase and ta_plnum = $pl_num ";
	$query .= "and ta_meeting = 0 ";
	$query .= "order by al_phase, al_pstart, mi_land, ta_id";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Der von Ihnen gew&auml;hlte Plan $pl_num enth&auml;lt keine Tasks!");
	   closeDB($db);
	   return;
	}

	$rows = 0;
	$sum = 0.0;
	$total = 0.0;
	$j = 0;
	$m = 0;
	$r = 0;
	$oldland = "";

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $al_hours = $data[0];
	   $al_pstart = $data[1];
	   $al_phase = $data[2];
	   $ta_name = $data[3];
	   $ta_notiz = $data[4];
	   $kl_land = $data[5];
	   $kp_phase = $data[6];

	   $jahr = gmdate("Y", $al_pstart);
	   $mon = gmdate("n", $al_pstart);

	   if ($rows == 0) {
	      $old['jahr'] = $jahr;
	      $old['mon'] = $mon;
	      $old['land'] = $kl_land;
	      $old['phase'] = $al_phase;
	      $old['tphase'] = $kp_phase;
	      $old['name'] = $ta_name;
	      $oldnotiz = $ta_notiz;
	   }

	   if ($old['jahr'] != $jahr || $old['mon'] != $mon || $old['land'] != $kl_land || $old['phase'] != $al_phase || $old['name'] != $ta_name) {
	      if ($phase)
	         echo "<tr><td>" . $old['phase'] . " " . $old['tphase'] . "</td>";
	      else
	         echo "<tr>";

	      echo "<td>" . $old['name'] . "</td>";
	      echo "<td>$oldnotiz</td>";
	      echo "<td>" . $old['land'] . "</td>";
	      echo "<td align=\"center\">" . $old['mon'] . "</td>";
	      echo "<td align=\"center\">" . $old['jahr'] . "</td>";
	      printf ("<td align=\"right\">%s</td></tr>\n", FormatNum($sum, 3));
	      $sum = 0;
	      $old['jahr'] = $jahr;
	      $old['mon'] = $mon;
	      $old['land'] = $kl_land;
	      $old['phase'] = $al_phase;
	      $old['tphase'] = $kp_phase;
	      $old['name'] = $ta_name;
	      $oldnotiz = "";
	      $r = $rows;
	   }

	   $sum += $al_hours;
	   $total += $al_hours;

	   if (strlen($ta_notiz)) {
	      if (strlen($oldnotiz))
	         $oldnotiz .= "<br>";

	      $oldnotiz .= $ta_notiz;
	   }

	   $rows++;
	}

	if ($r != $rows) {
	   if ($phase)
	      echo "<tr><td>$al_phase $kp_phase</td>";
	   else
	      echo "<tr>";

	   echo "<td>$ta_name</td>";
	   echo "<td>$ta_notiz</td>";
	   echo "<td>$kl_land</td>";
	   echo "<td align=\"center\">$mon</td>";
	   echo "<td align=\"center\">$jahr</td>";
	   printf ("<td align=\"right\">%s</td></tr>\n", FormatNum($sum, 3));
	}

	# Anzeigen der Summe
	if ($phase)
	   $span = 6;
	else
	   $span = 5;

	printf ("<tr><td colspan=$span class=\"stbb\">Summe:</td><td align=\"right\"><b>%s</b></td></tr>\n", FormatNum($total, 3));
	echo "</table>\n";
	closeDB($db);
}

function CreateTempPlan($db, $pl_num, $prnum) {
	global $leneinheit;

	# Als erstes erzeugen wir eine temporaere Tabelle in die wir die
	# einzelnen Daten unseres Plans zusammentragen und korrekt zuordnen.
	#
	$query = "create local temporary table TempPlan (";
	$query .= "tmp_lfd integer not null unique, tmp_miname integer,";
	$query .= "tmp_phase integer, tmp_periode integer, tmp_plan double precision,";
	$query .= "tmp_ist double precision, tmp_taname varchar(50), tmp_notiz varchar(8192),";
	$query .= "constraint \"tempplan_pkey\" primary key (\"tmp_lfd\"))";
	$result = QueryDB($db, $query);

	if (!$result)
	   return false;

	# Nun erzeugen wir einen Plan, ausgehend von den Tasks. Das ist der
	# erste von insgesamt zwei Schritten!
	#
	$query = "select al_hours, al_pstart, al_phase, ta_name, ta_notiz,";
	$query .= "al_ressource from allocation, task where ";
	$query .= "ta_num = al_task and ta_plnum = $pl_num ";
	$query .= "and ta_meeting = 0";
	$result = QueryDB($db, $query);

	if (!$result)
	   return false;

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Der von Ihnen gew&auml;hlte Plan $pl_num enth&auml;lt keine Tasks!");
	   return false;
	}

	$rows = 0;
	$zaehler = 1;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $al_hours = $data[0];
	   $al_pstart = $data[1];
	   $al_phase = $data[2];
	   $ta_name = $data[3];
	   $ta_notiz = $data[4];
	   $al_ressource = $data[5];

	   $mon = gmdate("n", $al_pstart);
	   $year = gmdate("Y", $al_pstart);
	   $al_pstart = gmmktime(0, 0, 0, $mon, 1, $year);

	   $query = "insert into TempPlan (tmp_lfd, tmp_miname, tmp_phase,";
	   $query .= "tmp_periode, tmp_plan, tmp_ist, tmp_taname, tmp_notiz) ";
	   $query .= "values ($zaehler, $al_ressource, $al_phase, $al_pstart,";
	   $query .= "$al_hours, 0, '$ta_name', '$ta_notiz')";

	   if (!QueryDB($db, $query))
	      return false;

	   $rows++;
	   $zaehler++;
	}

	# Nun suchen wir alle IST-Meldungen und ergaenzen die Daten, sofern
	# sie bereits vorhanden sind, oder fuegen einen neuen Datensatz ein.
	#
	$query = "select wd_minum, wd_datum, wd_hours / $leneinheit, wd_phase, ";
	$query .= "ta_name from wdone, task where ";
	$query .= "wd_prnum = $prnum and ta_num = wd_task";
	$result = QueryDB($db, $query);

	if (!$result)
	   return false;

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Der von Ihnen gew&auml;hlte Plan $pl_num enth&auml;lt keine IST-Buchungen!");
	   return true;
	}

	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $wd_minum = $data[0];
	   $wd_datum = $data[1];
	   $wd_ist = $data[2];
	   $wd_phase = $data[3];
	   $ta_name = $data[4];

	   $mon = gmdate("n", $wd_datum);
	   $year = gmdate("Y", $wd_datum);
	   $per = gmmktime(0, 0, 0, $mon, 1, $year);
	   $query = "select tmp_lfd, tmp_ist from TempPlan where ";
	   $query .= "tmp_miname = $wd_minum and tmp_periode = $per ";
	   $query .= "and tmp_taname = '$ta_name'";
	   $result2 = QueryDB($db, $query);

	   if (!$result2)
	      return false;

	   $nr = numrowsDB($result2);

	   if ($nr > 0) {
	      $data = fetchDB($result2, 0);
	      $tmp_lfd = $data[0];
	      $tmp_ist = $data[1];

	      $wd_ist += $tmp_ist;

	      $query = "update TempPlan set tmp_ist = $wd_ist ";
	      $query .= "where tmp_lfd = $tmp_lfd";

	      if (!QueryDB($db, $query))
	         return false;
	   } else {
	      $mon = gmdate("n", $wd_datum);
	      $year = gmdate("Y", $wd_datum);
	      $wd_datum = gmmktime(0, 0, 0, $mon, 1, $year);

	      $query = "insert into TempPlan (tmp_lfd, tmp_miname, tmp_phase,";
	      $query .= "tmp_periode, tmp_plan, tmp_ist, tmp_taname) ";
	      $query .= "values ($zaehler, $wd_minum, $wd_phase, $wd_datum,";
	      $query .= "0, $wd_ist, '$ta_name')";

	      if (!QueryDB($db, $query))
	         return false;

	      $zaehler++;
	   }

	   $rows++;
	}

	return true;
}

function ShowPlanDetail($print=false) {
	global $leneinheit;
	global $phase;
	$pl_num = $_REQUEST['pl_num'];

	$db = OpenDB();
	# Ermitteln der Projektnummer
	$query = "select pl_prnum from plan where pl_num = $pl_num";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$prnum = $data[0];

	# Folgende Funktion erzeugt uns einen Plan mit zugehoerigen IST-Werten,
	# wobei diese bereits innerhalb einer Periode aufsummiert wurden.
	#
	if (CreateTempPlan($db, $pl_num, $prnum) == false) {
	   closeDB($db);
	   return;
	}

	# Tabellenkopf
?>
<table width="99%" border=1>
   <tr>
<?
	if ($phase) {
	   echo "<td class=\"stbb\">Phase</td>\n";
	   echo "<td class=\"stbb\">Subphasen</td>\n";
	} else {
	   echo "<td class=\"stbb\">Task</td>\n";
	}
?>
      <td class="stbb">Aktivit&auml;t/T&auml;tigkeit</td>
      <td class="stbb">Ressource</td>
      <td class="stbb">Gruppe</td>
      <td class="stbb">Monat</td>
      <td class="stbb">Jahr</td>
      <td class="stbb">Ist</td>
      <td class="stbb">Rest</td>
      <td class="stbb">RM&nbsp;aktuell</td>
   </tr>
<?php
	# Auslesen der zuvor temporaer erzeugten Tabelle und Darstellung
	# der Inhalte.
	#
	$query = "select tmp_miname, tmp_phase, tmp_periode, tmp_plan,";
	$query .= "tmp_ist, tmp_taname, tmp_notiz, mi_nname, mi_vname,";
	$query .= "kl_land, kp_phase from TempPlan, mitarbeiter, key_land,";
	$query .= "key_phase where mi_num = tmp_miname and kl_num = mi_land ";
	$query .= "and kp_num = tmp_phase ";
	$query .= "order by tmp_phase, tmp_periode, mi_nname, mi_vname";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Der von Ihnen gew&auml;hlte Plan $pl_num enth&auml;lt keine Tasks!");
	   closeDB($db);
	   return;
	}

	$rows = 0;
	$sum = 0.0;
	$total = 0.0;
	$tothour = 0.0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $tmp_miname = $data[0];
	   $tmp_phase = $data[1];
	   $tmp_periode = $data[2];
	   $tmp_plan = $data[3];
	   $tmp_ist = $data[4];
	   $tmp_taname = $data[5];
	   $tmp_notiz = $data[6];
	   $mi_nname = $data[7];
	   $mi_vname = $data[8];
	   $kl_land = $data[9];
	   $kp_phase = $data[10];

	   $mon = gmdate("n", $tmp_periode);
	   $jahr = gmdate("Y", $tmp_periode);

	   # Schreiben der Zeile
	   if ($phase)
	      echo "<tr><td>$tmp_phase $kp_phase</td>";
	   else
	      echo "<tr>";

	   echo "<td>$tmp_taname</td>";
	   echo "<td>$tmp_notiz</td>";
	   echo "<td>$mi_nname $mi_vname</td>";
	   echo "<td>$kl_land</td>";
	   echo "<td align=\"center\">$mon</td>";
	   echo "<td align=\"center\">$jahr</td>";
	   printf ("<td align=\"right\">%s</td>", FormatNum($tmp_ist, 3));
	   $h = $tmp_plan - $tmp_ist;

	   if ($h < 0)
	      printf ("<td align=\"right\" class=\"five\">%s</td>", FormatNum($h, 3));
	   else
	      printf ("<td align=\"right\">%s</td>", FormatNum($h, 3));

	   if ($tmp_ist == 0 && $tmp_plan > 0)
	      printf ("<td align=\"right\" class=\"green\">%s</td></tr>\n", FormatNum($tmp_plan, 3));
	   else
	      printf ("<td align=\"right\">%s</td></tr>\n", FormatNum($tmp_plan, 3));

	   $tothour += $tmp_plan;
	   $total += $h;
	   $sum += $tmp_ist;
	   $rows++;
	}

	# Anzeigen der Summen
	if ($phase)
	   $span = 7;
	else
	   $span = 6;

	echo "<tr><td colspan=$span class=\"stbb\">Summe:</td>";
	printf("<td align=\"right\"><b>%s</b></td>", FormatNum($sum, 3));
	printf("<td align=\"right\"><b>%s</b></td>", FormatNum($total, 3));
	printf("<td align=\"right\"><b>%s</b></td></tr>\n", FormatNum($tothour, 3));
	echo "</table>\n";
	closeDB($db);
}

# Auswertung des Menues:
#
if ($knopf == 25)		// Planungshilfe
   Preselect();

# Auswertung der Funktionen
if ($func == "prplan") {
   $pl_num = $_REQUEST['pl_num'];

   # Ermitteln des Projektnamens und Anzeigen des selben.
   $db = OpenDB();
   $query = "select pl_prnum, pr_name from plan, project ";
   $query .= "where pr_num = pl_prnum and pl_num = $pl_num";
   $result = QueryDB($db, $query);
   $data = fetchDB($result, 0);
   $selpro = $data[0];
   $pr_name = $data[1];
   echo "<p class=\"cry\">Projekt: $selpro $pr_name</p>";
   echo "<br>\n";
   closeDB($db);

   echo "<a href=\"#\" onClick=\"javascript:open_mwindow('prplanhilf.php?pl_num=$pl_num&header=2&func=PrintPlan', 'Planungshilfe: $selpro $pr_name', 750, 550)\">";
   echo "<img src=\"image/print.png\" border=0 alt=\"Drucken\"></a>\n";

   ShowPlanGesamt();
   ShowPlanBasis();
   ShowPlanDetail();
}

if ($func == "PrintPlan") {
   $pl_num = $_REQUEST['pl_num'];

   # Ermitteln des Projektnamens und Anzeigen des selben.
   $db = OpenDB();
   $query = "select pl_prnum, pr_name from plan, project ";
   $query .= "where pr_num = pl_prnum and pl_num = $pl_num";
   $result = QueryDB($db, $query);
   $data = fetchDB($result, 0);
   $selpro = $data[0];
   $pr_name = $data[1];
   echo "<p class=\"cry\">Projekt: $selpro $pr_name</p>";
   echo "<br>\n";
   closeDB($db);

   ShowPlanGesamt(true);
   ShowPlanBasis(true);
   ShowPlanDetail(true);
}

require('footer.inc');
?>
